package pl.easyshop.core.common.selectable.helper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Pawel Witkowski
 *
 * @param <T>
 */
public class SelectableItemHelper<T> implements Serializable {
	
	/** Unique serialization identifier. */
	private static final long serialVersionUID = 3909100649448431009L;
	
	private List<T> items;
	private int selectedIndex;
	private Object initialSelection;

	public SelectableItemHelper (T[] items) {
		if(items == null) {
			this.items = Collections.emptyList();
		} else {
			this.items = Arrays.asList(items);	
		}
		
	}
	
	public SelectableItemHelper (Collection<T> items) {
		if(items == null) {
			this.items = Collections.emptyList();
		} else {
			this.items = new ArrayList<T>(items);
		}
		
	}

	public SelectableItemHelper<T> withSelection(T selection) {
		if (selection == null) {
			selectedIndex = 0;
		} else {
			this.selectedIndex = items.indexOf(selection);
			initialSelection = selection;
		}
		return this;
	}

	/*public SelectionControl sort() {
		Collections.sort(items);
		withSelection(initialSelection);
		return this;
	}*/

	/*public SelectionControl sort(Comparator comparator) {
		Collections.sort(items, comparator);
		withSelection(initialSelection);
		return this;
	}
*/
	/*public SelectionControl reverse() {
		Collections.reverse(items);
		withSelection(initialSelection);
		return this;
	}*/

	public List<?> getItems() {
		return items;
	}

	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public Object getSelection() {
		return items.get(selectedIndex);
	}
}